package ruler.rules.storage;

import java.util.ArrayList;
import java.util.HashMap;

import ruler.rules.Rule;
import ruler.rules.Rule.DEVICE_TYPE;

public class RulesHash {
	
	private HashMap<String, Rule> wifiEnableRules;
	private HashMap<String, Rule> wifiDisableRules;
	private HashMap<String, Rule> gpsEnableRules;
	private HashMap<String, Rule> gpsDisableRules;

	public RulesHash() {
		this.wifiEnableRules = new HashMap<String, Rule>();
		this.wifiDisableRules = new HashMap<String, Rule>();
		this.gpsEnableRules = new HashMap<String, Rule>();
		this.gpsDisableRules = new HashMap<String, Rule>();
	}

	public void addRule(Rule r) {
		if (r.getDeviceType().equals(DEVICE_TYPE.WIFI))
			if (r.getDeviceEnable())
				this.wifiEnableRules.put(r.getName(), r);
			else
				this.wifiDisableRules.put(r.getName(), r);
		else if (r.getDeviceEnable())
			this.gpsEnableRules.put(r.getName(), r);
		else
			this.gpsDisableRules.put(r.getName(), r);
	}

	public void removeRule(String wanted, DEVICE_TYPE device, boolean state) {
		if (device.equals(DEVICE_TYPE.WIFI))
			if (state)
				this.wifiEnableRules.remove(wanted);
			else
				this.wifiDisableRules.remove(wanted);
		else if (state)
			this.gpsEnableRules.remove(wanted);
		else
			this.gpsDisableRules.remove(wanted);
	}

	public ArrayList<Rule> getRules(DEVICE_TYPE device, boolean state) {
		ArrayList<Rule> result;
		if (device.equals(DEVICE_TYPE.WIFI))
			if (state) {
				result = new ArrayList<Rule>(this.wifiEnableRules.values());
			} else {
				result = new ArrayList<Rule>(this.wifiDisableRules.values());
			}
		else if (state) {
			result = new ArrayList<Rule>(this.gpsEnableRules.values());
		} else {
			result = new ArrayList<Rule>(this.gpsDisableRules.values());
		}
		return result;
	}

	public ArrayList<Rule> getAllRules(){
		ArrayList<Rule> result= new ArrayList<Rule>(this.wifiEnableRules.values());
		result.addAll(this.wifiDisableRules.values());
		result.addAll(this.gpsEnableRules.values());
		result.addAll(this.gpsDisableRules.values());		
		return result;
	}
}